package screen.component
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import helper.Services;
	import helper.ValueObject;

	public class Rank extends UI
	{
		private const _USER_AVATAR_FOLDER:String ="../assets/avatar/user/";
		private const RANK_GROUP:String = "rankGroup";
		private const RANK_UNIT_WIDTH:Number = 180;
		private const WEEK_BTN_NAME:String ="week";
		private var _rank_popup:MovieClip;
		private var _current_rank_group:MovieClip;
		private var _current_btn:MovieClip ;
		private var _get_top_loader:URLLoader;
		private var _service:Services;
		public function Rank()
		{
			init()
		}
		private function init():void{
			_rank_popup = ValueObject.S1_RESOURCE.getExportedAsset("rankPopup") as MovieClip;
			for(var i:Number=1;i<9;i++){
				var mc:MovieClip = _rank_popup.getChildByName(WEEK_BTN_NAME+i.toString()) as MovieClip;
				buttonImageInit(mc);
				mc.addEventListener(MouseEvent.CLICK,getTop);
				var rank_group:MovieClip = new MovieClip();
				rank_group.name = RANK_GROUP + i.toString();
				rank_group.x = _rank_popup.maskMc.x;
				rank_group.y = _rank_popup.maskMc.y;
				_rank_popup.addChild(rank_group);
				sliderInit(rank_group,4,1,rank_group.x,RANK_UNIT_WIDTH);
			}
			_current_rank_group = _rank_popup.getChildByName(RANK_GROUP + "1") as MovieClip;
			with(_rank_popup){
				buttonImageInit(nextBtn);
				MovieClip(nextBtn).addEventListener(MouseEvent.CLICK,nextHandle);
				buttonImageInit(preBtn);
				MovieClip(preBtn).addEventListener(MouseEvent.CLICK,preHandle);
				buttonImageInit(closeBtn);
				MovieClip(closeBtn).addEventListener(MouseEvent.CLICK, closeHandle);
			}
			popupInit(_rank_popup);
			
			_get_top_loader = new URLLoader();
			_service = Services.getInstance();
		}
		private function getTop(e:MouseEvent=null):void{
			if(_get_top_loader.hasEventListener(Event.COMPLETE)){
				return;
			}
			_get_top_loader.addEventListener(Event.COMPLETE,getTopComplete);
			_get_top_loader.addEventListener(IOErrorEvent.IO_ERROR,getTopError);
			
			if(e){
				_current_btn = e.currentTarget as MovieClip;
			}else if(!_current_btn){
				_current_btn = _rank_popup.getChildByName(WEEK_BTN_NAME + "1") as MovieClip;
			}
			var week:String = String(_current_btn.name).substring(4,5);
			_get_top_loader.load(_service.getTopRequest(week));
			
			for(var i:Number =1;i<9;i++){
				var rank_group:MovieClip = _rank_popup.getChildByName(RANK_GROUP + i.toString()) as MovieClip;
				rank_group.visible = false;
				var week_btn:MovieClip = _rank_popup.getChildByName(WEEK_BTN_NAME + i.toString()) as MovieClip;
				buttonImageEnabel(week_btn);
			}
			buttonImageDisable(_current_btn);
			_current_rank_group = _rank_popup.getChildByName(RANK_GROUP + week) as MovieClip;
			_current_rank_group.visible = true;
			_current_rank_group.mask = _rank_popup.maskMc
		}
		private function getTopComplete(e:Event):void{
			_get_top_loader.removeEventListener(Event.COMPLETE,getTopComplete);
			_get_top_loader.removeEventListener(IOErrorEvent.IO_ERROR, getTopError);
			var ob : Object = JSON.decode(e.target.data);
			_rank_popup.sumPlayerTf.text = ""+ob.totalUsers;
			_rank_popup.rankTf.text = ""+ob.rank;			
			var arr:Array = ob.top;
			while(_current_rank_group.numChildren>0){
				_current_rank_group.removeChildAt(0);
			}
			var leng:Number = arr.length;
			for(var i:Number =0; i<leng;i++){
				var rank_unit:MovieClip = ValueObject.S1_RESOURCE.getExportedAsset("rankUnit") as MovieClip;
				rank_unit.x = i*RANK_UNIT_WIDTH;
				TextField(rank_unit.rankTf).text = arr[i].number;
				TextField(rank_unit.nameTf).text = arr[i].name;
				TextField(rank_unit.starTf).text = arr[i].star;
				//rank_unit.avatar.load(new URLRequest(_USER_AVATAR_FOLDER + arr[i].userId +".png"));
				_current_rank_group.addChild(rank_unit);
			}
			updateSliderBtn(_current_rank_group);
		}
		private function getTopError(e:IOErrorEvent):void{
			_get_top_loader.removeEventListener(Event.COMPLETE,getTopComplete);
			_get_top_loader.removeEventListener(IOErrorEvent.IO_ERROR,getTopError);
			trace("getTopError");
			
		}
		private function nextHandle(e:MouseEvent):void{
			sliderNextClick(_current_rank_group);
		}
		private function preHandle(e:MouseEvent):void{
			sliderPreClick(_current_rank_group);
		}
		public function closeHandle(e:MouseEvent=null):void{
			popupHide(_rank_popup);
		}
		public function showRank():void{
			getTop();
			popupShow(_rank_popup);
		}
	}
}